<?php
namespace common\jobs;

use common\services\TallyService;
use Yii;
use yii\base\BaseObject;
use yii\queue\JobInterface;

/**
 * Class NewUserJob
 *
 * @package common\jobs
 * @desc 用户创建成功后的异步任务队列
 */
class NewUserJob extends BaseObject implements JobInterface
{
    /**
     * @var $uid integer 用户id
     */
    public $uid;

    /**
     * @param \yii\queue\Queue $queue
     * @return mixed|void
     * @throws
     * @desc 用户新增后触发的异步任务
     */
    public function execute($queue)
    {
        // 创建默认账本
        $this->initDefaultTally();
    }

    /**
     * @return integer $tallyId 账本ID
     * 创建默认账本
     */
    public function initDefaultTally()
    {
        $tallyId = TallyService::create([
            'uid'   => $this->uid,
            'title' => '账本示例',
            'company_title' => '',
            'memo' => '该示例由系统默认创建',
            'express_time'=>strtotime(date('Y-m-d')) + 86400 * 7
        ]);
        if($tallyId){
            // 创建异步队列 处理默认账本类别等默认数据填充
            Yii::$app->queue->push(new NewTallyJob([
                'tallyId' => $tallyId
            ]));
        }
        return $tallyId ;
    }
}

